/*
 moxl - Modular XML Library
 Copyright (c) 2007, Malcolm Sparks

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package moxl.parser.markup.states;

import moxl.parser.ParserException;
import moxl.parser.assemblers.comment.CommentAssembler;
import moxl.parser.assemblers.doctypedecl.DocumentTypeDeclarationAssembler;
import moxl.parser.assemblers.openelement.OpenElementAssembler;
import moxl.parser.assemblers.xmldecl.XmlDeclarationAssembler;
import moxl.parser.markup.Markup;
import moxl.parser.markup.types.OpenElementTagMarkup;
import moxl.tokenizer.Token;
import moxl.tokenizer.TokenStream;
import moxl.tokenizer.TokenizerException;
import moxl.tokenizer.xml.XmlTokenType;

public class StartStateDelegate implements XmlDocumentParserStateDelegate
{
	public Markup parseNextMarkupFromStream(TokenStream tstream, XmlParserContext ctx) throws ParserException, TokenizerException
	{
		Token nextToken = tstream.lookahead(1);
		XmlTokenType type = (XmlTokenType) nextToken.getType();

		switch (type)
		{
		case OPEN_CHEVRON_QUESTION_MARK:
		{
			Token tokenAfterNext = tstream.lookahead(2);

			if (tokenAfterNext.isType(XmlTokenType.NAME) && tokenAfterNext.getImage().equals("xml"))
			{
				XmlDeclarationAssembler assembler = new XmlDeclarationAssembler();
				Markup markup = assembler.assembleMarkupFromStream(tstream);
				ctx.changeState(XmlParserState.POST_XML_DECL_STATE);
				return markup;
			}
			else
			{
				// We must be dealing with a PI
				throw new UnsupportedOperationException("PI parsing not supported by this parser at " + tstream.lookahead(1).getLocation());
			}
		}

		case OPEN_COMMENT:
		{
			Markup markup = new CommentAssembler().assembleMarkupFromStream(tstream);
			ctx.changeState(XmlParserState.POST_XML_DECL_STATE);
			return markup;
		}

		case OPEN_CHEVRON:
		{
			OpenElementAssembler assembler = new OpenElementAssembler();
			OpenElementTagMarkup markup = (OpenElementTagMarkup) assembler.assembleMarkupFromStream(tstream);
			if (markup.isEmpty())
			{
				// That's our document element read, so we are in postamble
				ctx.changeState(XmlParserState.POSTAMBLE);
			}
			else
			{
				ctx.changeState(XmlParserState.ELEMENT_CONTENT);
			}
			return markup;
		}

		case OPEN_CHEVRON_EXCLAMATION_MARK:
		{
			// This must be a doctypedecl
			DocumentTypeDeclarationAssembler assembler = new DocumentTypeDeclarationAssembler();
			Markup markup = assembler.assembleMarkupFromStream(tstream);
			ctx.changeState(XmlParserState.POST_XML_DECL_STATE);
			return markup;
		}

		case SPACE:
		case WHITESPACE:
			// Whitespace is allowed, but puts us in a post xml-decl state
			throw new UnsupportedOperationException("Must eat up this whitespace at " + tstream.lookahead(1).getLocation());

		default:
			throw new ParserException("Unexpected token: " + nextToken, nextToken.getLocation());
		}
	}

}
